package com.honour.controller.interfaced;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.FriendInManager;
import com.live.service.interfaced.impl.LiveInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.PageData;
import com.system.util.Tools;

/**
 @Function
 @Author 于泓磊
 @Date   2017年12月4日
*/

@Controller
@RequestMapping(value="/appfriendin")
public class FriendInController extends BaseController{
	
	@Resource(name="friendinService")
	private FriendInManager friendinService;
	
	@Resource(name="liveInService")
	private LiveInService liveInService;
	
	
	/**
	 * 好友管理添加好友(加关注)
	 * @Author 于泓磊
	 * @date   2017年12月4日
	 * @return
	 */
	@RequestMapping(value="/addfriend")
	@ResponseBody
	public Object addFriend(){
		logBefore(logger, "好友管理添加好友(加关注)接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("FRIENDADD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addFriend", pd)){	//检查参数
					//获取关系为好友的FRIENDID
					pd.put("FRIENDNAME", "好友");
					PageData p = friendinService.getFriendId(pd);
					//添加到好友列表
					pd.put("FRIENDLIST_ID", this.getSnowId());
					pd.put("FRIENDID", p.get("FRIENDID"));
					friendinService.addFriend(pd);
					PageData page=liveInService.getTaskStatus(pd);
					if (page.getInterger("TASK_ATTENTION")==0) {
						//判断如果关注人数大于5则视为完成任务,更新数据库任务属性
						if (liveInService.getUserData(pd).getInterger("FOCUSCOUNT")>4) {
							pd.put("TASK_ATTENTION", 1);
							pd.put("BLACKCOIN", 30);
							liveInService.updTaskStatus(pd);
						}
						
					}
					result = (null == pd) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 获取我关注的好友列表
	 * @Author 于泓磊
	 * @date   2017年12月5日
	 * @return
	 */
	@RequestMapping(value="/friendlist")
	@ResponseBody
	public Object getFriendList(){
		logBefore(logger, "获取我关注的好友列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		PageData p2 = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("FLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getFriendList", pd)){	//检查参数
					//获取关系为好友的FRIENDID
					pd.put("FRIENDNAME", "好友");
					PageData p = friendinService.getFriendId(pd);
					pd.put("FRIENDID", p.get("FRIENDID"));
					List<PageData> list = friendinService.getFriendList(pd);
					for (PageData pageData : list) {
						String REHONOURUSER_ID=(String) pageData.get("REHONOURUSER_ID");
					    p2.put("REHONOURUSER_ID", REHONOURUSER_ID);
					    p2= friendinService.getFansCount(p2);
					    pageData.put("FANSCOUNT", p2.get("FANSCOUNT"));
					}
					if (list.size()!=0) {
						map.put("pd", list);
					}
					result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 获取关注我的好友列表
	 * @Author 于泓磊
	 * @date   2017年12月5日
	 * @return
	 */
	@RequestMapping(value="/refriendlist")
	@ResponseBody
	public Object getReFriendList(){
		logBefore(logger, "获取关注我的好友列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		PageData p2 = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("REFLIST", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getFriendList", pd)){	//检查参数
					//获取关系为好友的FRIENDID
					pd.put("FRIENDNAME", "好友");
					PageData p = friendinService.getFriendId(pd);
					pd.put("FRIENDID", p.get("FRIENDID"));
					pd.put("REHONOURUSER_ID", pd.get("HONOURUSER_ID"));
					List<PageData> list = friendinService.getReFriendList(pd);
					for (PageData pageData : list) {
						String HONOURUSER_ID=(String) pageData.get("HONOURUSER_ID");
					    p2.put("HONOURUSER_ID", HONOURUSER_ID);
					    p2= friendinService.getReFansCount(p2);
					    pageData.put("FANSCOUNT", p2.get("FANSCOUNT"));
					}
					if (list.size()!=0) {
						map.put("pd", list);
					}
					result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 取消关注
	 * @Author 于泓磊
	 * @date   2017年12月5日
	 * @return
	 */
	@RequestMapping(value="/delfriend")
	@ResponseBody
	public Object delFriend(){
		logBefore(logger, "获取关注我的好友列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("FRIENDDEL", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addFriend", pd)){	//检查参数
					//删除好友
					friendinService.delFriend(pd);
					result =   "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
}
